package com.sparrow.mvc.ui;

import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

import com.sparrow.constant.CONFIG;
import com.sparrow.utils.Config;

@SuppressWarnings("serial")
public class JStyleLink extends TagSupport {
	private String href;

	public String getHref() {
		return href;
	}

	public void setHref(String href) {
		this.href = href;
	}

	@Override
	public int doStartTag() throws JspException {
		int returnValue = TagSupport.SKIP_BODY;
		String writeHTML = "";
		writeHTML = "<link rel=\"stylesheet\" type=\"text/css\"  href=\"";
		if (this.getHref().contains("$resource")) {
			writeHTML += this.getHref().replace("$resource",
					Config.getValueInCache(CONFIG.RESOURCE));
		} else if (this.getHref().contains("$rootPath")) {
			writeHTML += this.getHref().replace("$rootPath",
					Config.getValueInCache(CONFIG.ROOT_PATH));
		}
		writeHTML += "?v="
				+ Config.getValueInCache(CONFIG.RESOURCE_VERSION)
				+ "\"";
		try {
			if (!writeHTML.trim().equals("")) {
				this.pageContext.getOut().print(writeHTML + "></link>");
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return returnValue;
	}
}
